//
//  XSPullDownControl.m
//  绿地管理系统
//
//  Created by rimi on 16/1/28.
//  Copyright © 2016年 hexs. All rights reserved.
//

#import "XSPullDownControl.h"

static const CGFloat cellHeight_ = 30;

@interface XSPullDownControl () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSArray *options;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation XSPullDownControl


- (instancetype)initWithFrame:(CGRect)frame optionsList:(NSArray *)options
{
    self = [super initWithFrame:frame];
    if (self) {
        self.options = options;
        [self initializeAppearance];
    }
    return self;
}

- (void)initializeAppearance
{
    [self addTarget:self action:@selector(didClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.titleLabel];
}

- (void)didClick
{
    self.selected = !self.selected;
    if (self.selected) {
        // 展开
        [self.superview addSubview:self.tableView];
        NSInteger count = self.options.count;
        if (count > 4) {
            count = 4;
        }
        CGFloat height = count * cellHeight_;
        
        [UIView animateWithDuration:0.3 animations:^{
            CGRect frame = self.tableView.frame;
            frame.size.height = height;
            self.tableView.frame = frame;
        }];
    } else {
        // 收起
        [self close];
    }
}
- (void)close
{
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame = self.tableView.frame;
        frame.size.height = 0;
        self.tableView.frame = frame;
    } completion:^(BOOL finished) {
        [self.tableView removeFromSuperview];
    }];
}

- (void)clear
{
    self.titleLabel.text = nil;
    _selectedIndex = -1;
}

#pragma mark -- tableView protocol

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.options.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdf" forIndexPath:indexPath];
    cell.textLabel.text = self.options[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.titleLabel.text = self.options[indexPath.row];
    [self close];
    self.selected = !self.selected;
    _selectedIndex = indexPath.row;
    if (self.clickAction) {
        self.clickAction(self);
    }
}

#pragma mark -- getter
// 1. 因为tableView加到了self的层级上，高于self层级的视图将盖住这个tableView (视图层级，tableView - 出现就应该盖住下面的其他视图)
// 2. tableView 超过了self的范围，无法点击(如果tableView加到了self的层级上，tableView超过了self的范围，无法点击)
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(self.frame.origin.x, CGRectGetMaxY(self.frame), CGRectGetWidth(self.frame), 0) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellIdf"];
        _tableView.rowHeight = cellHeight_;
    }
    return _tableView;
}

- (UILabel *)titleLabel
{
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] initWithFrame:self.bounds];
    }
    return _titleLabel;
}

- (NSString *)selectedOption
{
    return self.titleLabel.text;
}

- (NSInteger)selectedIndex
{
    return _selectedIndex;
}
@end










